package web;

import ejb.Articolo;
import ejb.BusinessLogic;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Danilo
 */
@WebServlet(name = "ModificaProdotto", urlPatterns = {"/ModificaProdotto"})
public class ModificaProdotto extends HttpServlet {
    @EJB
    private BusinessLogic businessLogic;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
      

        List<Articolo> a = businessLogic.listaArticoliOrdinati();
                int i = 0;
                int flag = 0;
                while (i < a.size() && flag == 0) {
                    String radiobutton = request.getParameter("radio");
                    if (radiobutton != null && radiobutton.equals(""+i)) {
                        out.println("<form class='ins_prodotto' method='post' action='amministrazione.jsp'>"
                                + "<fieldset id='sign'>"
                                + "<legend>Inserisci Articolo</legend>"
                                + "<div class='dati_prod'>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext1'>Marca:</label>"
                                + "<input class='inputtext' type='text' name='marca' value='"+a.get(i).getMarca() +"' "
                                + "readonly='readonly' />"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext1'>Tipo:</label>"
                                + "<input class='inputtext' type='text' name='tipo' value='"+a.get(i).getTipo() +"'    + readonly='readonly' />"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext2'>Modello:</label>"
                                + "<input class='inputtext' type='text' name='modello' value='"+a.get(i).getModello() +"' "
                                + "readonly='readonly' />"
                                + "</div>"
                                + "</div>"
                                + "<div class='dati_prod'>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext2'>Taglia:</label>"
                                + "<input class='inputtext' type='text' name='taglia' value='"+a.get(i).getTaglia() +"' />"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext1'>Disponibilit&agrave;:</label>"
                                + "<input class='inputtext' type='text' name='disp' value='"+a.get(i).getQuantita() +"' />"
                                + "</div>"
                                + "</div>"
                                + "<div class='dati_prod'>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext2'>Prezzo_acquisto:</label>"
                                + "<input class='inputtext' type='text' name='pr_acquisto' value='"+a.get(i).getPrezzo_acquisto() +"' />"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext1'>Prezzo_vendita:</label>"
                                + "<input class='inputtext' type='text' name='pr_vendita' value='"+a.get(i).getPrezzo_vendita() +"' />"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext1'>Path Foto:</label>"
                                + "<input class='inputtext' type='text' name='foto' value='"+a.get(i).getFoto() +"' />"
                                + "</div>"
                                + "</div>"
                                + "<div class='campo_ins_prod'>"
                                + "<label for='inputtext2'>Descrizione:</label>"
                                + "<textarea name='descr'  ' cols=100% rows=10>"+a.get(i).getDescrizione()+"</textarea>"
                                + "<input type='hidden' name='id_mod' value='"+a.get(i).getId()+"'/>"
                                + " </div>"
                                + "<input id='inputsubmit1' type='submit' name='modifica2' value='Modifica' />"
                                + "</fieldset>"
                                + "</form>");
                        flag = 1;
                    }
                    i++;
                }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
